package com.example.batterynotification;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.util.Log;

// the serviece is used to register the reciever in it and hence, the app. 
//will be listening to battery change all the time
public class ListeningService extends Service{

	
	batteryLevelReceiver batInfo;
	private int highNotificationLevel;
	private int lowNotificationLevel;	
	private int    level ;	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		Log.i("LC", "Oncreate");
		super.onCreate();		
	}

	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {		
		Log.i("LC", "OnstartCommand");
		AppContext appContext = new AppContext(this);

		

		Log.i("LC", "after create the notification");
		batInfo = new batteryLevelReceiver();
		IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent ReceiverIntent =registerReceiver(batInfo, mIntentFilter);	
		 BatteryReceiverForegroundResources.setBatteryIntent(ReceiverIntent);
		 String Timer;		 
		 
		  highNotificationLevel = Preferences.getIntPreferences("prefHighBatteryLevel");
		  lowNotificationLevel  =Preferences.getIntPreferences("prefLowhBatteryLevel"); 			
 		  
		 
		 if(ReceiverIntent !=null)
		 {
	     	  level  = ReceiverIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); 
			 if(ReceiverIntent.getIntExtra(BatteryManager.EXTRA_STATUS, 0)== BatteryManager.BATTERY_STATUS_DISCHARGING)
			 {

				 Timer=Preferences.getStringPreferences("DischargingTimer");				 
				 if(Timer!=null)
				 {
					 int iTimer = Integer.parseInt(Timer);
						ManageNotifications.createNotification(Util.MilliSecondsConvert(iTimer*(level-lowNotificationLevel)));					 
					 
				 }
				 else
				 {
						ManageNotifications.createNotification("Calculating...");					 
				 }
			 }
			 else if(ReceiverIntent.getIntExtra(BatteryManager.EXTRA_STATUS, 0)== BatteryManager.BATTERY_STATUS_CHARGING)
			 {
				 Timer=Preferences.getStringPreferences("ChargingTimer");

				 if(Timer!=null)
				 {
					 int iTimer = Integer.parseInt(Timer);
					 
					 int iRemainingtime=iTimer*(highNotificationLevel-level);
					 Log.i("TI","Timer Not="+iTimer +"highNotificationLevel-level"+(highNotificationLevel-level));					
					 Log.i("TI","OnstartCommand timer=" +iRemainingtime);						 
						ManageNotifications.createNotification(Util.MilliSecondsConvert(iRemainingtime));					 
					 
				 }	
				 else
				 {
						ManageNotifications.createNotification("Calculating...");					 
				 }				 
			 }
			 
		 }
		 
			new Preferences(appContext);
			new ManageNotifications(appContext);

			
		Preferences.savePreferences("isServiceStarted", true);		
		Log.i("LC", "service started");
		return super.onStartCommand(intent, flags, startId);
	}


	@Override
	public void onDestroy() {
		AppContext appContext = new AppContext(this);
		new Preferences(appContext);
		Log.i("LC", "SERVICE STOPPED");
		ManageNotifications.removeNotification();
			
		unregisterReceiver(batInfo);	
		Preferences.savePreferences("isServiceStarted", false);	
		super.onDestroy();
		

	}

}
